<?php
require "../common/user_online.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>EbookAZ</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<?php require "../components/head.php" ?>
</head>
<body onload="MakeCurrentPage(4)">
<div id="container">
	<?php require "../components/user_menu.php" ?>
	<?php require "../components/user_header.php" ?>
	<div id="content">
		<?php require "../components/content_left.php"?>
		<div id="content_right">
<?php

include "../common/db.inc";
$mysqli = new mysqli($host, $username, $password,$dbname);
$mysqli->set_charset('utf8');
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$pagesize = 12;
$currentpage = 1;
$numdisplay = 5;

if (isset($_GET["page"]))
	$currentpage = $_GET["page"];
$start = ($currentpage-1) * $pagesize;
	
$userid = $_SESSION['user_id'];
$query = ("call sp_GetDownloadByUser('$userid', '$start', '$pagesize')");

$CountEbook = 0;
echo "<h1 align='center'>Ebook đã download</h3>";

if ($result = $mysqli->query($query)) 
{
    $i=0;
	$count =  mysqli_num_rows($result);
	echo "<table border='1' cellspacing='0'>";
    while ($row = $result->fetch_row()) 
	{
		$title = $row[1];
		$img = $image.$row[8];	
		$mana = $row[13];
		$downloadcount = $row[11];		
		$rating = $row[14];
		$download_date = $row[15];
		$arr = date_parse($download_date);
		$ngay = $arr['day'] >= 10 ? $arr['day'] : "0" . $arr['day'];
		$thang = $arr['month'] >= 10 ? $arr['month'] : "0" . $arr['month'];
		$nam = $arr['year'];
		$h = $arr['hour'] >= 10 ? $arr['hour'] : "0" . $arr['hour'];
		$m = $arr['minute'] >= 10 ? $arr['minute'] : "0" . $arr['minute'];
		$s = $arr['second'] >= 10 ? $arr['second'] : "0" . $arr['second'];
		
		$date = "$ngay/$thang/$nam $h:$m:$s";
		if($i%4 == 0)
			echo "<tr>";
		echo "<td width='25%' valign='top'>";
		echo "<table><tr>";
		echo "<td valign='top'>";
		echo "<a href='XemChiTietSach.php?id=$row[0]'><img align='left' src='$img' width='60px' height='100px' alt='EBook'/></a>";
		echo "</td><td valign='top'>";
		echo "<a href='XemChiTietSach.php?id=$row[0]'>$title</a><br/>";
		echo "<label>$date</label><br/>";
		echo "<label><i>Mana: </i>$mana</label><br/>";
		echo "<label><i>Tải về: </i>$downloadcount</label><br/>";
		echo "<label><i>Đánh giá: </i>$rating</label>";
		echo "</td></tr></table>";
		echo "</td>";
		if($i % 4 == 3)
			echo "</tr>";
		else if($i == $count-1)
		{
			for($j=0;$j<4-($count%4);$j++)
				echo "<td>&nbsp;</td>";
			echo "</tr>";
		}
		$i++;
	}
	 $result->close();
}
echo "</table>";	

include '../common/paging.php';
$count = 0;
$mysqli = new mysqli($host, $username, $password, $dbname);
$query = "call sp_CountDownloadByUser('$userid')";
$result = $mysqli->query($query);
$row = $result->fetch_row();
$count = $row[0];

echo paging('DanhSachEbookDown.php', $pagesize, $numdisplay, $count, $currentpage, '');


$mysqli->close();

?>
		</div>
	</div>
	<?php require "../components/footer.php" ?>
</div>
</body>
</html>
